﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MineDiscover.Utils;

namespace MineDiscover.Forms
{
    public partial class NewGameForm : Form
    {
        public NewGameForm()
        {
            InitializeComponent();
            this.btnOK.Click += new EventHandler(btnOK_Click);   
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DifficultlyEventArgs diff = new DifficultlyEventArgs();

            if (rbAdvandced.Checked)
            {
                diff.Difficluty = RuleManager.GetDifficultDefault(DifficultType.Advanced);
            }

            if (rbIntermediate.Checked)
            {
                diff.Difficluty = RuleManager.GetDifficultDefault(DifficultType.Intermediate);
            }

            if (rbBeginner.Checked)
            {
                diff.Difficluty = RuleManager.GetDifficultDefault(DifficultType.Beginner);
            }

            if (rbCustom.Checked)
            {
                int row = Int32.Parse(txtRow.Text);
                int column = Int32.Parse(txtColumn.Text);
                int mine = Int32.Parse(txtMine.Text);

                if (mine >= row * column)
                {
                    MessageBox.Show("Mine is so many !");
                    return;
                }

                if (row > 120 || column > 120)
                {
                    MessageBox.Show("Rows and Columns < 120 !");
                    return;
                }

                diff.Difficluty = RuleManager.GetDifficultCustom(row, column, mine);
            }

            InvokeNewGame(diff);
        }

        public event EventHandler<DifficultlyEventArgs> NewGameClick;

        private void InvokeNewGame(DifficultlyEventArgs diff)
        {
            var handler = NewGameClick;
            if (handler != null)
            {
                handler(this, diff);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtRow_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (ValidateIsDigit(e))
            {
                e.Handled = true;
            }
        }

        private void txtColumn_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (ValidateIsDigit(e))
            {
                e.Handled = true;
            }
        }

        private void txtMine_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (ValidateIsDigit(e))
            {
                e.Handled = true;
            }
        }

        private bool ValidateIsDigit(KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                return true;
            }
            return false;
        }
    }
}
